package webservices;


import java.util.List;
import dao.DAOFactory;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET; 
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path; 
import javax.ws.rs.PathParam; 
import javax.ws.rs.Produces; 
import javax.ws.rs.core.Context;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType; 
import javax.ws.rs.core.Request;
import javax.ws.rs.core.UriInfo;

import sun.security.krb5.Credentials;
import classes.Market;
import classes.Tip;


@Path("/tipovi")
public class TipResource {
	@Context
	 UriInfo uriInfo;
	 @Context
	 Request request;
	 
	 
	 @Path("/tip/{id}")
		@GET
		@Produces(MediaType.APPLICATION_XML)
		public Tip vratiTip(@PathParam("id")String sIDTipa) {
			try {
				int idTipa = Integer.parseInt(sIDTipa);
				DAOFactory.connect();
				Tip t = DAOFactory.getTipDAO().selectTip(idTipa);
				DAOFactory.disconnect();
				return t;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
		}
	 
	 @Path("/svi")
		@GET
		@Produces(MediaType.APPLICATION_XML)
		public GenericEntity<List<Tip>> vratiTipove() {
			try {
				DAOFactory.connect();
				List<Tip> tipovi = DAOFactory.getTipDAO().selectSviTipovi();
				DAOFactory.disconnect();
				return new GenericEntity<List<Tip>>(tipovi){};
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
		}
	 
	 
	 @PUT
	  @Path("tip/novi")
	  @Consumes(MediaType.APPLICATION_XML)
	  @Produces(MediaType.APPLICATION_XML)
	  public String putTip(@HeaderParam("username")String username, @HeaderParam("password")String password, Tip t) throws Exception {
		  try{
			  DAOFactory.connect();
			  DAOFactory.getTipDAO().insertTip(t);
			  DAOFactory.disconnect();
			  return Boolean.toString(true);
		  }
		  catch(Exception e){
			  e.printStackTrace();
			  return Boolean.toString(false);
		  }
	  }
	 
	 
	 @POST
	  @Path("tip/obrisi")
	  @Consumes(MediaType.APPLICATION_XML)
	  @Produces(MediaType.APPLICATION_XML)
	  public String deleteTipPOST(@HeaderParam("username")String username, @HeaderParam("password")String password, String sIDTipa){
		  try{
			  int idTipa = Integer.parseInt(sIDTipa);
			  DAOFactory.connect();
			  int i = DAOFactory.getTipDAO().deleteTip(idTipa);
			  if (i!=1){
				  DAOFactory.disconnect();
				  return Boolean.toString(false);
			  }
			  DAOFactory.disconnect();
			  return Boolean.toString(true);
		  }
		  catch(Exception e){
			  e.printStackTrace();
			  return Boolean.toString(false);
		  }
	  }
	 
	 @DELETE
	  @Path("tip/obrisi")
	  @Consumes(MediaType.APPLICATION_XML)
	  @Produces(MediaType.APPLICATION_XML)
	  public void deleteTip(@HeaderParam("username")String username, @HeaderParam("password")String password, @HeaderParam("id")String sIDTipa){
		  try{
			  int idTipa = Integer.parseInt(sIDTipa);
			  DAOFactory.connect();
			  int i = DAOFactory.getTipDAO().deleteTip(idTipa);
			  if (i!=1){
				  DAOFactory.disconnect();
			  }
			  DAOFactory.disconnect();
			/**  int idKorisnikaAdmina = KontrolerPristupa.getID(username, password, Korisnik.Privilegije.Administrator);
			  if (idKorisnikaAdmina!=-1){ 
				  int i = DAOFactory.getKorisnikDAO().deleteKorisnik(idKorisnika);
				  if (i!=1){
					  DAOFactory.disconnect();
					  return Boolean.toString(false);
				  }
				  Logger.ZabiljeziAkciju(idKorisnikaAdmina, "Obrisao korisnika sa ID: "+sIDKorisnika);
				  DAOFactory.disconnect();
				  return Boolean.toString(true);
			  }
			  return Boolean.toString(false);*/
		  }
		  catch(Exception e){
			  e.printStackTrace();
		  }
	  }


}
